package com.briup.product_source.service.impl;

import com.briup.product_source.dao.ManagerFenceHouseMapper;
import com.briup.product_source.dao.ext.ManagerHurdlesExtMapper;
import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.ManagerFenceHouse;
import com.briup.product_source.pojo.ext.ManagerFenceHouseExt;
import com.briup.product_source.result.ResultCode;
import com.briup.product_source.service.FenceHouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class FenceHouseServiceImpl implements FenceHouseService {

    @Autowired
    private ManagerFenceHouseMapper  managerFenceHouseMapper;

    @Autowired
    private ManagerHurdlesExtMapper managerHurdlesExtMapper;

    @Override
    public PageInfo<ManagerFenceHouse> findByPage(Integer pageNum, Integer pageSize, String fhName) {

        // PageHelper开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 核心查询语句
        List<ManagerFenceHouse> managerFenceHouses = managerFenceHouseMapper.queryAllHouses(fhName);
        // 将查询的信息封装在PageInfo对象中，返回
        PageInfo<ManagerFenceHouse> pageInfo = new PageInfo<>(managerFenceHouses);
        return pageInfo;
    }

    @Override
    public void insertOrUpdate(ManagerFenceHouse managerFenceHouse) {
        // 判断参数是否为空
       String name = managerFenceHouse.getFhName();
       if(managerFenceHouse.getFhTime() == null || !StringUtils.hasText(name)){
           throw new ServiceException(ResultCode.PARAM_IS_EMPTY);
       }
        String id = managerFenceHouse.getFhId();
        ManagerFenceHouse mf1 = managerFenceHouseMapper.selectByPrimaryKey(id);
        int result;
       if(StringUtils.hasText(id)){
           if(managerFenceHouseMapper.selectByPrimaryKey(id) == null){
               throw new ServiceException(ResultCode.DATA_IS_EMPTY);
           }
           if(mf1 == null && !mf1.getFhId().equals(id)){
               throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
           }
           result = managerFenceHouseMapper.updateByPrimaryKey(managerFenceHouse);
       }else {
           if(id == null){
               //设置编号
               managerFenceHouse.setFhId(UUID.randomUUID().toString().replace("-",""));
               result = managerFenceHouseMapper.insert(managerFenceHouse);
           }else {
               throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
           }
       }
       if(result == 0){
           throw new ServiceException(ResultCode.FAIL);
       }
    }

    @Override
    public void deleteById(String id) {
        ManagerFenceHouse managerFenceHouse = managerFenceHouseMapper.selectByPrimaryKey(id);
        //判断是否存在
        if(managerFenceHouse != null){
            if(managerHurdlesExtMapper.selectCountByFhId(id) > 0){
                throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
            }else {
                managerFenceHouseMapper.deleteById(id);
            }
        }else {
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
    }

    @Override
    public void deleteByIdAll(List<String> ids) {
        List<String> list = new ArrayList<>();
        int empty = 0;
        int rel = 0;
        for(String id : ids){
            if(managerFenceHouseMapper.selectByPrimaryKey(id) != null){
                if(managerHurdlesExtMapper.selectCountByFhId(id) == 0){
                    list.add(id);
                }else {
                    rel ++;
                }
            }else {
                empty ++;
            }
        }
        if(empty == ids.size()){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        if(rel == ids.size()){
            throw new ServiceException(ResultCode.DATA_CAN_NOT_DELETE);
        }
        if(list.isEmpty()){
            throw new ServiceException(ResultCode.FAIL);
        }
        managerFenceHouseMapper.deleteByIdAll(list);
    }

    @Override
    public ManagerFenceHouseExt findById(String id) {
        ManagerFenceHouseExt managerFenceHouseExt = managerFenceHouseMapper.selectHouseAndHurdlesById(id);
        if (managerFenceHouseExt == null) {
            throw new ServiceException(ResultCode.FAIL);
        }
        return managerFenceHouseExt;
    }

    @Override
    public List<ManagerFenceHouse> findAll() {
        return managerFenceHouseMapper.findAll();
    }
}
